package com.why.connector.session;

import com.why.configurebean.DefaultConfiguration;
import com.why.executor.Executor;

import java.util.List;

public class DefaultSqlSession implements SqlSession {
    private DefaultConfiguration defaultConfiguration;
    private Executor executor;

    public DefaultSqlSession(DefaultConfiguration defaultConfiguration, Executor sqlSession) {
        this.defaultConfiguration = defaultConfiguration;
        this.executor = sqlSession;
    }

    @Override
    public <E> List<E> selectList(String statement) {
        return (List<E>) executor.query(defaultConfiguration.getClassMappingMapper().get(statement), null);
    }

    @Override
    public <E> List<E> selectList(String statement, Object parameter) {
        return (List<E>) executor.query(defaultConfiguration.getClassMappingMapper().get(statement), parameter);
    }


    @Override
    public <T> T selectOne(String statement, Object parameter) {
        List<T> list = (List<T>) executor.query(defaultConfiguration.getClassMappingMapper().get(statement), parameter);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}